package com.letao.service;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import com.letao.dao.CategoryDAO;
import com.letao.dao.EvaluationDAO;
import com.letao.dao.GoodsDAO;
import com.letao.dao.GoodsImageDAO;
import com.letao.dao.UserCollectionDAO;
import com.letao.data.LocationAlgorithm;
import com.letao.pojo.Category;
import com.letao.pojo.Evaluation;
import com.letao.pojo.Goods;
import com.letao.pojo.Transaction;
import com.letao.pojo.User;
import com.letao.pojo.UserCollection;

public class GoodsService implements GoodsServiceInterface {
	private GoodsDAO goodsDao;
	private CategoryDAO categoryDao;
	private EvaluationDAO evaluationDAO;
	private UserCollectionDAO userCollectionDao;
	private GoodsImageDAO goodsImageDAO;
	@Override
	public boolean addGoods(Goods goods) throws Exception {
		// TODO Auto-generated method stub
		goodsDao.save(goods);
		return true;
	}

	@Override
	public boolean deleteGoods(Goods goods) throws Exception {
		// TODO Auto-generated method stub
		goodsDao.delete(goods);
		return true;
	}

	@Override
	public boolean updateGoods(Goods goods) throws Exception {
		// TODO Auto-generated method stub
		goodsDao.attachDirty(goods);
		return true;
	}
	@Override
	public boolean addCategory(Category category) throws Exception {
		// TODO Auto-generated method stub
		categoryDao.save(category);
		return true;
	}

	@Override
	public boolean deleteCategory(Category category) throws Exception {
		// TODO Auto-generated method stub
		categoryDao.delete(category);
		return true;
	}

	@Override
	public boolean updateCategory(Category category) throws Exception {
		// TODO Auto-generated method stub
		categoryDao.attachDirty(category);
		return true;
	}

	@Override
	public List<Category> getAllCategory() throws Exception {
		// TODO Auto-generated method stub
		
		return categoryDao.findAll();
	}

	@Override
	public List<Goods> getGoodsByCategory(Integer Cid) throws Exception {
		// TODO Auto-generated method stub
		
		return goodsDao.findByProperty("category.cid", Cid);
	}

	@Override
	public Goods getGoodsByGoodsId(Integer Gid) throws Exception {
		// TODO Auto-generated method stub
		return goodsDao.findById(Gid);
	}

	@Override
	public List<Evaluation> getEvaluationByGoodsId(Integer Gid) throws Exception {
		// TODO Auto-generated method stub
		return evaluationDAO.findByProperty("goods.gid", Gid);
	}

	@Override
	public List<Goods> getGoodsByHot() throws Exception {
		// TODO Auto-generated method stub
		List<Goods> finalGoodsList=new ArrayList<Goods>();
		List<Goods> goodsList=goodsDao.findAll();
		Collections.sort(goodsList, new Comparator() {  
	          public int compare(Object a, Object b) {  
	            int one = ((Goods)a).getGcount();  
	            int two = ((Goods)b).getGcount();    
	            return two- one ;   
	          }  
	       });
		for(int i=0;i<goodsList.size()&&i<5;i++){
				finalGoodsList.add(goodsList.get(i));
		}
			
		return finalGoodsList;
	}

	@Override
	public List<Goods> getGoodsByOwer(Integer Uid) throws Exception {
		// TODO Auto-generated method stub
		return goodsDao.findByProperty("user.uid", Uid);
	}

	@Override
	public List<Goods> getGoodsByLocation(String location,Double r) throws Exception {
		// TODO Auto-generated method stub
		List<Goods> finalGoodList=new ArrayList<Goods>();
		List<Goods> goodsList=goodsDao.findAll();
		for(Goods goods:goodsList){
			if(LocationAlgorithm.isNear(location, goods.getGaddress(), r))
				finalGoodList.add(goods);
		}
		return finalGoodList;
	}

	@Override
	public List<Goods> getGoodsByCategoryAndLocation(Integer Cid, String location,Double r)
			throws Exception {
		// TODO Auto-generated method stub
		List<Goods> finalGoodList=new ArrayList<Goods>();
		List<Goods> goodsList=goodsDao.findByProperty("category.cid", Cid);
		for(Goods goods:goodsList){
			if(LocationAlgorithm.isNear(location, goods.getGaddress(), r))
				finalGoodList.add(goods);
		}
		return finalGoodList;
	}

	@Override
	public List<Goods> getGoodsByNameAndLocation(String Gname, String location,Double r)
			throws Exception {
		// TODO Auto-generated method stub
		List<Goods> finalGoodList=new ArrayList<Goods>();
		List<Goods> goodsList=goodsDao.findByGname(Gname);
		for(Goods goods:goodsList){
			if(LocationAlgorithm.isNear(location, goods.getGaddress(), r))
				finalGoodList.add(goods);
		}
		return finalGoodList;
	}
	public GoodsDAO getGoodsDao() {
		return goodsDao;
	}

	public void setGoodsDao(GoodsDAO goodsDao) {
		this.goodsDao = goodsDao;
	}

	public CategoryDAO getCategoryDao() {
		return categoryDao;
	}

	public void setCategoryDao(CategoryDAO categoryDao) {
		this.categoryDao = categoryDao;
	}

	public EvaluationDAO getEvaluationDAO() {
		return evaluationDAO;
	}

	public void setEvaluationDAO(EvaluationDAO evaluationDAO) {
		this.evaluationDAO = evaluationDAO;
	}

	

	public UserCollectionDAO getUserCollectionDao() {
		return userCollectionDao;
	}

	public void setUserCollectionDao(UserCollectionDAO userCollectionDao) {
		this.userCollectionDao = userCollectionDao;
	}

	@Override
	public boolean addCollection(UserCollection userCollection)
			throws Exception {
		// TODO Auto-generated method stub
		userCollectionDao.save(userCollection);
		return true;
	}

	@Override
	public boolean deleteCollection(UserCollection userCollection)
			throws Exception {
		// TODO Auto-generated method stub
		userCollectionDao.delete(userCollection);
		return true;
	}

	@Override
	public boolean updateCollection(UserCollection userCollection)
			throws Exception {
		// TODO Auto-generated method stub
		userCollectionDao.attachDirty(userCollection);
		return true;
	}

	@Override
	public List<UserCollection> getUserCollectionByUser(Integer Uid)
			throws Exception {
		// TODO Auto-generated method stub
		return userCollectionDao.findByProperty("user.uid", Uid);
	}

	@Override
	public boolean addGoodsImage(Goods goods,List<File> fileList) throws Exception {
		// TODO Auto-generated method stub
		goodsImageDAO.save(goods, fileList);
		return true;
	}

	public GoodsImageDAO getGoodsImageDAO() {
		return goodsImageDAO;
	}

	public void setGoodsImageDAO(GoodsImageDAO goodsImageDAO) {
		this.goodsImageDAO = goodsImageDAO;
	}

	

}
